package com.toefl.speaking.activity;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.toefl.speaking.R;
import com.toefl.speaking.activity.base.BaseActivity;
import com.toefl.speaking.dao.SQLiteHelper;
import com.toefl.speaking.handler.LoginHandler;
import com.toefl.speaking.handler.LoginHandler.Login;
import com.toefl.speaking.handler.RegisterHandler;
import com.toefl.speaking.handler.RegisterHandler.Register;
import com.toefl.speaking.model.UserInfo;

public class IndexActivity extends BaseActivity {
	private final String TAG = "IndexActivity";

	private Button btn_login;
	private Button btn_register;
	private Button btn_forget_pass, btn_find_pass_canel;
	private Button register_ok;
	private Button register_cancel;
	private RelativeLayout login_view, register_view, forget_view;
	/** 登录信息 */
	private EditText user_name_text, user_pass_text;
	/** 注册信息 */
	private EditText new_user_pass, new_user_re_pass, new_user_mail, new_user_name;

	private boolean isLogin = false, isRegister = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.context = IndexActivity.this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		ButtonsOnClickListener listener = new ButtonsOnClickListener();
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_register = (Button) findViewById(R.id.btn_register);
		login_view = (RelativeLayout) findViewById(R.id.login_view);
		register_view = (RelativeLayout) findViewById(R.id.register_view);
		register_ok = (Button) findViewById(R.id.register_ok);
		register_cancel = (Button) findViewById(R.id.register_cancel);
		btn_forget_pass = (Button) findViewById(R.id.btn_forget_pass);
		btn_find_pass_canel = (Button) findViewById(R.id.btn_find_pass_canel);
		forget_view = (RelativeLayout) findViewById(R.id.forget_view);

		user_name_text = (EditText) findViewById(R.id.user_name_text);
		user_pass_text = (EditText) findViewById(R.id.user_pass_text);

		new_user_pass = (EditText) findViewById(R.id.new_user_pass);
		new_user_re_pass = (EditText) findViewById(R.id.new_user_re_pass);
		new_user_mail = (EditText) findViewById(R.id.new_user_mail);
		new_user_name = (EditText) findViewById(R.id.new_user_name);

		btn_login.setOnClickListener(listener);
		btn_register.setOnClickListener(listener);
		register_ok.setOnClickListener(listener);
		register_cancel.setOnClickListener(listener);
		btn_forget_pass.setOnClickListener(listener);
		btn_find_pass_canel.setOnClickListener(listener);

	}

	@Override
	protected void initData() {
		SQLiteHelper.init(this); // 初始化数据库
		super.initData();
	}

	private class ButtonsOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_login:
				isLogin = true;
				Intent intent = new Intent();
				intent.setClass(IndexActivity.this, MainActivity.class);
				IndexActivity.this.finish();
				startActivity(intent);
				// getUserAsyncTask().execute();
				break;
			case R.id.btn_register:
				login_view.setVisibility(View.GONE);
				register_view.setVisibility(View.VISIBLE);
				forget_view.setVisibility(View.GONE);
				break;
			case R.id.register_ok:
				isRegister = true;
				getUserAsyncTask().execute();
				break;
			case R.id.register_cancel:
				login_view.setVisibility(View.VISIBLE);
				register_view.setVisibility(View.GONE);
				forget_view.setVisibility(View.GONE);
				break;
			case R.id.btn_forget_pass:
				login_view.setVisibility(View.GONE);
				register_view.setVisibility(View.GONE);
				forget_view.setVisibility(View.VISIBLE);
				break;
			case R.id.btn_find_pass_canel:
				login_view.setVisibility(View.VISIBLE);
				register_view.setVisibility(View.GONE);
				forget_view.setVisibility(View.GONE);
				break;
			}
		}
	}

	@Override
	protected void userOnPost() {
		if (isLogin) {
			Intent intent = new Intent();
			intent.setClass(IndexActivity.this, MainActivity.class);
			// IndexActivity.this.finish();
			startActivity(intent);
			isLogin = false;
		}
		if (isRegister) {
			Intent intent2 = new Intent();
			intent2.setClass(IndexActivity.this, MainActivity.class);
			startActivity(intent2);
			isRegister = false;
		}
		super.userOnPost();
	}

	@Override
	public void userDoInBackground() {
		if (isLogin) {
			postLogin();
		}
		if (isRegister) {
			postRegister();
		}
		super.userDoInBackground();
	}

	public void postLogin() {
		LoginHandler loginHandler = LoginHandler.instance();
		Login login = loginHandler.getLogin();
		login.setAccount(user_name_text.getText().toString());
		login.setPassword(user_pass_text.getText().toString());
		login.setKeepLogin(true);
		login.setDeviceToken("android_device_token");
		String result = null;

		try {
			result = loginHandler.postLogin(login, IndexActivity.this);
			JSONObject info = new JSONObject(result).getJSONObject("UserInfo");
			UserInfo userInfo = new UserInfo();
			userInfo.setUid(Integer.parseInt(info.getString("Uid")));
			userInfo.setName(info.getString("Name"));
			userInfo.setEmail(info.getString("Email"));
			Log.i(TAG, result);
		} catch (Exception e) {

		}
	}

	public void postRegister() {
		RegisterHandler registerHandler = RegisterHandler.instance();
		Register register = registerHandler.getRegister();
		register.setDeviceToken("asdasdfadsf");
		register.setEmail(new_user_mail.getText().toString());
		register.setPasswd(new_user_pass.getText().toString());
		register.setUserName(new_user_name.getText().toString());
		try {
			String result = registerHandler.postRegister(register, IndexActivity.this);
			System.out.println(result);
		} catch (Exception e) {

		}

	}

	public void postForgetPass() {

	}
}